
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
request.setAttribute("decorator", "none");
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0);
%>

<%-- 导入面包屑 --%>
<jsp:include page="../common/nav.jsp"></jsp:include>

<div class="wrapper wrapper-content animated fadeInRight">

	<%-- 导入提示信息框 --%>
    <c:if test="${hint!=null&&hint!=''}">
   		<jsp:include page="../common/alert/alert.jsp">
   			<jsp:param value="${hint}" name="alertType"/>
   			<jsp:param value="${alertMessage}" name="alertMessage"/>
   		</jsp:include>
    </c:if>

	<div class="row">
		<div class="col-lg-12">
			<div class="ibox ">
				<div class="ibox-title btn-success btn-outline panel-success collapse-link" title="展开/收起">
					<h5>生产任务&nbsp;<span style="color: black;">（<i class="fa fa-tag"></i>${requestScope.approveStatusMap[requestScope.prodWipTask.approveStatus]}）</span></h5>
					<div class="ibox-tools">
						<i class="fa fa-chevron-up"></i> 
					</div>
				</div>

				<div class="ibox-content border-bottom" style="padding-bottom: 0px;">
					<form id="form" action="web/prodWipTask/editProdWipTask" method="post">
						<div class="form-group  row">
							<label class="col-sm-2 col-form-label"><span class="text-danger">*</span><strong>任务编码</strong></label>
							<div class="col-sm-4">
								<input id="taskCode" name="taskCode" type="text" class="form-control" value="${requestScope.prodWipTask.taskCode}">
							</div>
							
							<label class="col-sm-2 col-form-label"><span class="text-danger">*</span><strong>任务名称</strong></label>
							<div class="col-sm-4">
								<input id="taskName" name="taskName" type="text" class="form-control" value="${requestScope.prodWipTask.taskName}">
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						
						<div class="form-group row">
	                        <label class="col-sm-2 col-form-label"><span class="text-danger">*</span><strong>任务类型</strong></label>
							<div class="col-sm-4">
		                        <select class="select2 form-control" name="taskType" id="taskType">
		                        	<option value="" selected="selected">请选择...</option>
		                        	<c:forEach items="${requestScope.taskTypeMap}" var="taskType">
		                        		<option value="${taskType.key}">${taskType.value}</option>
		                        	</c:forEach>
		                        </select>
	                        </div>
							
	                        <label class="col-sm-2 col-form-label"><strong>任务状态</strong></label>
							<div class="col-sm-4">
								<input id="taskStatusDesc" type="text" class="form-control" value="${requestScope.taskStatusMap[requestScope.prodWipTask.taskStatus]}" readonly="readonly">
							</div>
	                    </div>
	                    <div class="hr-line-dashed"></div>
						
						<div class="form-group row">
	                        <label class="col-sm-2 col-form-label"><span class="text-danger">*</span><strong>物料</strong></label>
	                        <div class="col-sm-4">
		                        <select class="select2 form-control" name="materialCode" id="materialCode">
		                        	<option value="" selected="selected">请选择...</option>
		                        	<c:forEach items="${requestScope.materialMap}" var="material">
		                        		<option value="${material.key}">${material.value}（${material.key}）</option>
		                        	</c:forEach>
		                        </select>
	                        </div>

							<label class="col-sm-2 col-form-label"><strong>物料规格</strong></label>
							<div class="col-sm-4">
								<input id="materialStandard" type="text" class="form-control" value="${requestScope.prodWipTask.materialStandard}" readonly="readonly">
							</div>
	                    </div>
	                    <div class="hr-line-dashed"></div>
	                    
	                    <div class="form-group row">
	                        <label class="col-sm-2 col-form-label"><span class="text-danger">*</span><strong>物料单位</strong></label>
							<div class="col-sm-4">
								<select class="select2 form-control" name="materialUnit" id="materialUnit">
		                        	<option value="" selected="selected">请选择...</option>
		                        	<c:forEach items="${requestScope.materialUnitMap}" var="materialUnit">
		                        		<option value="${materialUnit.key}">${materialUnit.value}</option>
		                        	</c:forEach>
		                        </select>
							</div>
							
							<label class="col-sm-2 col-form-label"><span class="text-danger">*</span><strong>生产数量</strong></label>
							<div class="col-sm-4">
								<input id="quantity" name="quantity" type="text" class="form-control" value="${requestScope.prodWipTask.quantity}">
							</div>
	                    </div>
	                    <div class="hr-line-dashed"></div>
	                    
	                    <div class="form-group row">
	                        <label class="col-sm-2 col-form-label"><span class="text-danger">*</span><strong>开工时间</strong></label>
	                        <div class="col-sm-4">
	                        	<div class="input-group date">
									<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
									<input id="startDate" name="startDate" type="text" class="form-control" value="${requestScope.prodWipTask.startDate}" autocomplete="off">
								</div>
	                        </div>
	                    
							<label class="col-sm-2 col-form-label"><strong>完工时间</strong></label>
							<div class="col-sm-4">
								<div class="input-group date">
									<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
									<input id="endDate" name="endDate" type="text" class="form-control" value="${requestScope.prodWipTask.endDate}" autocomplete="off">
								</div>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						
						<div class="form-group row">
	                        <label class="col-sm-2 col-form-label"><strong>物料清单</strong></label>
	                        <div class="col-sm-4">
	                        	<input id="bomHeadName" type="text" class="form-control" value="${requestScope.prodWipTask.bomHeadName}" readonly="readonly">
	                        </div>

	                        <label class="col-sm-2 col-form-label"><strong>物料清单版本</strong></label>
	                        <div class="col-sm-4">
	                        	<input id="bomVersion" type="text" class="form-control" value="${requestScope.prodWipTask.bomVersion}" readonly="readonly">
	                        </div>
	                    </div>
	                    <div class="hr-line-dashed"></div>
	                    
	                    <div class="form-group row">
	                        <label class="col-sm-2 col-form-label"><strong>工艺路线</strong></label>
	                        <div class="col-sm-4">
	                        	<input id="craftName" type="text" class="form-control" value="${requestScope.prodWipTask.craftName}" readonly="readonly">
	                        </div>

	                        <label class="col-sm-2 col-form-label"><strong>工艺路线版本</strong></label>
	                        <div class="col-sm-4">
	                        	<input id="craftVersion" type="text" class="form-control" value="${requestScope.prodWipTask.craftVersion}" readonly="readonly">
	                        </div>
	                    </div>
	                    <div class="hr-line-dashed"></div>
	                    
	                    <div class="form-group  row">
							<label class="col-sm-2 col-form-label"><strong>制单人</strong></label>
							<div class="col-sm-4">
								<input type="text" class="form-control" value="${requestScope.prodWipTask.staffName}" readonly="readonly">
							</div>
							
							<label class="col-sm-2 col-form-label"><strong>制单部门</strong></label>
							<div class="col-sm-4">
								<input type="text" class="form-control" value="${requestScope.prodWipTask.departmentName}" readonly="readonly">
							</div>
						</div>
						<div class="hr-line-dashed"></div>
	                    
						<div class="form-group row">
							<div class="col-sm-12 col-sm-offset-2 text-right">
								<button class="btn btn-white btn-lg" type="button" onclick="window.location.href='web/prodWipTask/getProdWipTaskList'">返回</button>&nbsp;
								<c:if test="${param.taskCode==null||param.taskCode==''||requestScope.prodWipTask.approveStatus=='UNSUBMIT'||requestScope.prodWipTask.approveStatus=='REJECT' }">
									<button class="ladda-button ladda-button-demo btn btn-success btn-lg" data-style="expand-right">&nbsp;&nbsp;保存&nbsp;&nbsp;<i class="fa fa-save"></i></button>
								</c:if>
								
								<c:if test="${param.taskCode!=null&&param.taskCode!=''}">
									<c:if test="${requestScope.workflowFlag!='Y'}">
									<c:if test="${requestScope.prodWipTask.approveStatus=='UNSUBMIT'||requestScope.prodWipTask.approveStatus=='REJECT' }">
										<button class="btn btn-primary btn-lg" type="button" onclick="submitData()">&nbsp;&nbsp;提交&nbsp;&nbsp;<i class="fa fa-arrow-circle-right"></i></button>&nbsp;
									</c:if>
									<c:if test="${requestScope.prodWipTask.approveStatus=='SUBMIT' }">
										<button class="btn btn-warning btn-lg btn-redragon-approve" type="button" onclick="approveData()">&nbsp;&nbsp;审核通过&nbsp;&nbsp;<i class="fa fa-check-circle"></i></button>&nbsp;
										<button class="btn btn-danger btn-lg btn-redragon-approve" type="button" onclick="rejectData()">&nbsp;&nbsp;驳回&nbsp;&nbsp;<i class="fa fa-times-circle"></i></button>&nbsp;
									</c:if>
									<c:if test="${requestScope.prodWipTask.approveStatus=='APPROVE'&&requestScope.prodWipTask.taskStatus!='FINISH'&&requestScope.prodWipTask.taskStatus!='CANCEL'}">
										<button class="btn btn-success btn-lg" type="button" onclick="alterData()">&nbsp;&nbsp;变更&nbsp;&nbsp;<i class="fa fa-retweet"></i></button>&nbsp;
										<c:if test="${requestScope.prodWipTask.taskStatus=='NOASSIGN' }">
											<button class="btn btn-success btn-lg" type="button" onclick="assignTask()">发放&nbsp;&nbsp;<i class="fa fa-level-down"></i></button>&nbsp;
										</c:if>
										<c:if test="${requestScope.prodWipTask.taskStatus=='ASSIGN' }">
											<button class="btn btn-danger btn-lg" type="button" onclick="cancelTask()">取消&nbsp;&nbsp;<i class="fa fa-times-rectangle"></i></button>&nbsp;
										</c:if>
									</c:if>
									</c:if>
									
									<c:if test="${requestScope.prodWipTask.approveStatus=='APPROVE'&&requestScope.prodWipTask.taskStatus=='FINISH'}">
										<button class="btn btn-outline btn-success" type="button" onclick="openTaskInvInput('${param.taskCode}')">查看入库单&nbsp;&nbsp;<i class="fa fa-paper-plane-o"></i></button>
										<button class="btn btn-outline btn-warning" type="button" onclick="openTaskInvOutput('${param.taskCode}')">查看退货单&nbsp;&nbsp;<i class="fa fa-paper-plane-o"></i></button>
									</c:if>
								</c:if>
								
								<!-- workflow -->
								<c:if test="${requestScope.workflowFlag=='Y'}">
								<c:if test="${param.taskCode!=null&&param.taskCode!=''}">
									<c:if test="${requestScope.prodWipTask.approveStatus=='UNSUBMIT'||requestScope.prodWipTask.approveStatus=='REJECT' }">
										<button id="workflowSubmitButton" class="btn btn-primary btn-lg" type="button" onclick="workflowSubmit('WIP','${param.taskCode}','${requestScope.prodWipTask.departmentCode}')">&nbsp;&nbsp;提交审批&nbsp;&nbsp;<i class="fa fa-arrow-circle-right"></i></button>&nbsp;
									</c:if>
									<c:if test="${requestScope.workflowControlFlag=='Y'}">
									<c:if test="${requestScope.prodWipTask.approveStatus=='SUBMIT' }">		
										<button id="workflowApproveButton" class="btn btn-warning btn-lg" type="button" onclick="workflowApprove('WIP','${param.taskCode}','${requestScope.prodWipTask.departmentCode}')">&nbsp;&nbsp;单据审核&nbsp;&nbsp;<i class="fa fa-check-circle"></i></button>&nbsp;
										<button id="workflowRejectButton" class="btn btn-danger btn-lg" type="button" onclick="workflowReject('WIP','${param.taskCode}','${requestScope.prodWipTask.departmentCode}')">&nbsp;&nbsp;单据驳回&nbsp;&nbsp;<i class="fa fa-times-circle"></i></button>&nbsp;
									</c:if>
									</c:if>
									<c:if test="${requestScope.prodWipTask.approveStatus=='APPROVE' }">
										<button class="btn btn-success btn-lg" type="button" onclick="alterData()">&nbsp;&nbsp;变更&nbsp;&nbsp;<i class="fa fa-retweet"></i></button>&nbsp;
									</c:if>
								</c:if>
								</c:if>
							</div>
						</div>
						
						<input type="hidden" id="taskId" name="taskId" value="${requestScope.prodWipTask.taskId}">
						<input type="hidden" id="bomHeadCode" name="bomHeadCode" value="${requestScope.prodWipTask.bomHeadCode}">
						<input type="hidden" id="craftCode" name="craftCode" value="${requestScope.prodWipTask.craftCode}">
						<input type="hidden" id="status" name="status" value="${requestScope.prodWipTask.status}">
						<input type="hidden" id="taskStatus" name="taskStatus" value="${requestScope.prodWipTask.taskStatus}">
						<input type="hidden" id="staffCode" name="staffCode" value="${requestScope.prodWipTask.staffCode}">
						<input type="hidden" id="departmentCode" name="departmentCode" value="${requestScope.prodWipTask.departmentCode}">
						<input type="hidden" name="createdDate" value="${requestScope.prodWipTask.createdDate}">
						<input type="hidden" name="createdBy" value="${requestScope.prodWipTask.createdBy}">
					</form>
				</div>

				<!-- tab 开始 -->
				<div class="row">
					<div class="col-lg-12">
						<div class="ibox ">
							<div class="ibox-title btn-info btn-outline panel-info">
								<h5>行信息</h5>
								<div class="ibox-tools">
								</div>
							</div>

							<div id="line-ibox-content" class="ibox-content border-bottom" style="padding-bottom: 0px;">
								<div class="tabs-container">
									<ul class="nav nav-tabs">
										<li><a class="nav-link active" data-toggle="tab" href="#materialTab" onclick="getMaterialLineTab('${requestScope.prodWipTask.taskCode}')">物料需求列表</a></li>
										<li><a class="nav-link" data-toggle="tab" href="#procedureTab" onclick="getProcedureLineTab('${requestScope.prodWipTask.taskCode}')">工序计划列表</a></li>
									</ul>
									<div id="tabDiv" class="tab-content">
									</div>
								</div>
							</div>
							
						</div>
					</div>
				</div>
				<!-- tab 结束 -->
				
			</div>
		</div>
	</div>
</div>

<!-- workflow -->
<script src="js/workflow.js"></script>
<div id="approveWorkflowModal"></div>

<!-- select2 -->
<script src="js/plugins/select2/select2.full.min.js"></script>

<script>
	$(document).ready(function() {
		//初始化taskType
		if("${requestScope.prodWipTask.taskType}"!=""){
			$("#taskType").val("${requestScope.prodWipTask.taskType}");
		}
		//初始化materialCode
		if("${requestScope.prodWipTask.materialCode}"!=""){
			$("#materialCode").val("${requestScope.prodWipTask.materialCode}");
		}
		//初始化materialUnit
		if("${requestScope.prodWipTask.materialUnit}"!=""){
			$("#materialUnit").val("${requestScope.prodWipTask.materialUnit}");
		}
		
		//初始化taskCode只读
		if("${requestScope.prodWipTask.taskCode}"!=""){
			$("#taskCode").prop("readonly", true);
		}
	
		//初始化select2
		$('.select2').select2({width: "100%"});
		
		//设置日期插件
		$('#startDate').datepicker({
			todayBtn : "linked",
			keyboardNavigation : true,
			forceParse : false,
			calendarWeeks : false,
			autoclose : true,
			format: 'yyyy-mm-dd',
			language: 'zh-CN',
		});
		
		//设置日期插件
		$('#endDate').datepicker({
			todayBtn : "linked",
			keyboardNavigation : true,
			forceParse : false,
			calendarWeeks : false,
			autoclose : true,
			format: 'yyyy-mm-dd',
			language: 'zh-CN',
		});
		
		//切换物料效果
		$("#materialCode").change(function(){
			getBomMaterialInfoAjax($("#materialCode").val());
			getMaterialBomAjax($("#materialCode").val());
			getMaterialCraftAjax($("#materialCode").val());
		});
		
		
		
		//表单提交
		var l = $('.ladda-button-demo').ladda();

		l.click(function() {
			$("#form").valid();
			//l.ladda('stop');
		});

		$("#form").validate({
			rules : {
				taskCode : {
					required : true,
					isCode : true,
				},
				taskName : {
					required : true,
				},
				taskType : {
					required : true,
				},
				materialCode : {
					required : true,
				},
				materialUnit : {
					required : true,
				},
				quantity : {
					required : true,
					number : true,
					gtZero : true,
				},
				startDate : {
					required : true,
				},
				taskStatus : {
					required : true,
				},
			},
			submitHandler: function(form) {
				var submitFlag = "Y";
				
				//验证字段
				if($("#taskType").val()=="STANDARD"){
					if($("#bomHeadCode").val()==""){
						submitFlag = "N";
						redragonJS.alert("标准离散任务的物料清单必填，请查看当前物料是否创建物料清单");
					}
					if($("#craftCode").val()==""){
						submitFlag = "N";
						redragonJS.alert("标准离散任务的工艺路线必填，请查看当前物料是否创建工艺路线");
					}
				}
				
				if(submitFlag=="Y"){
					l.ladda('start');
			        form.submit();
				}
		    }
		});
		
		//初始化tab
		getMaterialLineTab("${requestScope.prodWipTask.taskCode}");
	});
	
	
	//获取行tab
	function getMaterialLineTab(code){
		$.ajax({
			type: "post",
			url: "web/prodWipTaskMaterial/getProdWipTaskMaterialList",
			data: {"taskCode": code},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#tabDiv").html(data);
					$("#materialTab").addClass("active");
					//隐藏保存按钮
					if(($("#taskId").val()!=null&&$("#taskId").val()!=""&&("${requestScope.prodWipTask.approveStatus}"!="APPROVE"||"${requestScope.prodWipTask.taskStatus}"!="ASSIGN"))||$("#taskId").val()==null||$("#taskId").val()==""){
						$("#tabDiv .btn:not(.btn-redragon-adjust)").hide();
					}
					initControlAuth();
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
	//获取行tab
	function getProcedureLineTab(code){
		$.ajax({
			type: "post",
			url: "web/prodWipTaskProcedure/getProdWipTaskProcedureList",
			data: {"taskCode": code},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#tabDiv").html(data);
					$("#procedureTab").addClass("active");
					//隐藏保存按钮
					if(($("#taskId").val()!=null&&$("#taskId").val()!=""&&("${requestScope.prodWipTask.approveStatus}"!="APPROVE"||"${requestScope.prodWipTask.taskStatus}"!="ASSIGN"))||$("#taskId").val()==null||$("#taskId").val()==""){
						$("#tabDiv .btn:not(.btn-redragon-adjust)").hide();
					}
					initControlAuth();
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
	//提交审批
	function submitData(){
		window.location.href='web/prodWipTask/updateApproveStatus?code=${requestScope.prodWipTask.taskCode}&approveStatus=SUBMIT';
	}
	
	//审批通过
	function approveData(confirmFlag){
		if(confirmFlag=="N"){
			window.location.href='web/prodWipTask/updateApproveStatus?code=${requestScope.prodWipTask.taskCode}&approveStatus=APPROVE';
		}else{
			redragonJS.confirm("确认审批通过？", function(){
				window.location.href='web/prodWipTask/updateApproveStatus?code=${requestScope.prodWipTask.taskCode}&approveStatus=APPROVE';
			});
		}
	}
	
	//审批驳回
	function rejectData(){
		//redragonJS.confirm("确认审批驳回？", function(){
			window.location.href='web/prodWipTask/updateApproveStatus?code=${requestScope.prodWipTask.taskCode}&approveStatus=REJECT';
		//});
	}
	
	//数据变更
	function alterData(){
		redragonJS.confirm("确认变更数据？数据变更可能会影响到已有的业务！", function(){
			window.location.href='web/prodWipTask/updateApproveStatus?code=${requestScope.prodWipTask.taskCode}&approveStatus=UNSUBMIT';
		});
	}
	
	//任务发放
	function assignTask(){
		redragonJS.confirm("确认任务发放？", function(){
			window.location.href='web/prodWipTask/updateTaskStatus?code=${requestScope.prodWipTask.taskCode}&taskStatus=ASSIGN';
		});
	}
	
	//任务发放
	function cancelTask(){
		redragonJS.confirm("确认取消任务？取消后无法恢复", function(){
			window.location.href='web/prodWipTask/updateTaskStatus?code=${requestScope.prodWipTask.taskCode}&taskStatus=CANCEL';
		});
	}
	
	//获取物料信息
	function getBomMaterialInfoAjax(materialCode){
		$.ajax({
			type: "post",
			url: "web/prodBomHead/getBomMaterialInfoAjax",
			data: {"materialCode": materialCode},
			async: false,
			dataType: "json",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#materialUnit").val(data.materialUnitCode).trigger("change");
					$("#materialStandard").val(data.materialStandard);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
	//获取物料bom信息
	function getMaterialBomAjax(materialCode){
		$.ajax({
			type: "post",
			url: "web/prodWipTask/getMaterialBomAjax",
			data: {"materialCode": materialCode},
			async: false,
			dataType: "json",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#bomHeadCode").val(data.bomHeadCode);
					$("#bomHeadName").val(data.materialName+"物料清单");
					$("#bomVersion").val(data.bomVersion);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
	//获取物料工艺路线信息
	function getMaterialCraftAjax(materialCode){
		$.ajax({
			type: "post",
			url: "web/prodWipTask/getMaterialCraftAjax",
			data: {"materialCode": materialCode},
			async: false,
			dataType: "json",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#craftCode").val(data.craftCode);
					$("#craftName").val(data.craftName);
					$("#craftVersion").val(data.craftVersion);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
	function openTaskInvInput(code){
		window.open("web/invInputHead/getInvInputHeadList?inputSourceType=PROD&inputSourceHeadCode="+code+"&inputType=WIP_IN");
	}
	
	function openTaskInvOutput(code){
		window.open("web/invOutputHead/getInvOutputHeadList?outputSourceType=PROD&outputSourceHeadCode="+code+"&outputType=WIP_OUT");
	}
</script>